package Mapper;

import Utils.JdbcUtils;
import pojo.User;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserMapper {
    private JdbcUtils jdbcUtils = new JdbcUtils();

    public List<User> getAllUsers() throws SQLException {
        String sql = "select  * from user";
        Statement statement = jdbcUtils.getStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        List<User> userList = new ArrayList<>();
        while (resultSet.next()) {
            User user = new User();
            user.setUserid(resultSet.getInt("userid"));
            user.setUsername(resultSet.getString("username"));
            String password = resultSet.getString("password");
            user.setPassword(password);
            userList.add(user);
        }
        return userList;
    }
    public boolean login(String username, String password) throws SQLException {
        PreparedStatement prepareStatement = jdbcUtils.getPrepareStatement("select userid  from user where username = ? and password = ?");
        prepareStatement.setString(1,username);
        prepareStatement.setString(2, password);
        ResultSet resultSet = prepareStatement.executeQuery();
        if (resultSet.next()){
            return  true;
        }
        return  false;
    }
    public  int getUseridByUsername(String username) throws SQLException {
        Statement statement = jdbcUtils.getStatement();
        String sql = "select userid  from user where  username = '"+username+"'";
        ResultSet resultSet = statement.executeQuery(sql);
        resultSet.next();
        int userid = resultSet.getInt("userid");
        return userid;

    }
}
